//
//  CYMineHeaderView.m
//  cyydt
//
//  Created by chu on 2019/1/15.
//  Copyright © 2019 chu. All rights reserved.
//

#import "CYMineHeaderView.h"
#import "ZTRegisterViewController.h"

#import "CYMine_WoDeGuanZhuViewController.h"
#import "CYMine_MyCollectionViewController.h"
#import "CYMyHistoryViewController.h"

@interface CYMineHeaderView ()

@property (nonatomic, strong) UIView *upView;//上面视图

@property (nonatomic, strong) UIImageView *upImageView;//背景视图

@property (nonatomic, strong) UIView *upContentView;

@property (nonatomic, strong) UIView *bottomView;

@property (nonatomic, strong) NSMutableArray *btnsArr;


@property (nonatomic, strong) UIImageView *loginOrRegisImageView;

@end

@implementation CYMineHeaderView

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = BackGroundColor;
        [self initSubViews];
    }
    return self;
}

- (void)initSubViews{
    self.upView = [[UIView alloc] init];
    self.upView.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.upView];
    
    self.upImageView = [[UIImageView alloc] init];
    self.upImageView.image = [UIImage imageNamed:@"wd_bg"];
    [self.upView addSubview:self.upImageView];

    self.upContentView = [[UIView alloc] init];
    self.upContentView.backgroundColor = [UIColor whiteColor];
    self.upContentView.layer.cornerRadius = 10;
    self.upContentView.layer.masksToBounds = YES;
    UIImage *conteImage = [UIImage imageNamed:@"wd_bg2"];
    self.upContentView.layer.contents = (id)conteImage.CGImage;
    [self.upView addSubview:self.upContentView];
    
    //头像
    self.headImageView = [[UIImageView alloc] init];
    self.headImageView.image = [UIImage imageNamed:@"wd_bg"];
    self.headImageView.layer.cornerRadius = 50 / 2;
    self.headImageView.layer.masksToBounds = YES;
    [self.upContentView addSubview:self.headImageView];
    
    //昵称
    self.nicknameLabel = [[UILabel alloc] init];
    self.nicknameLabel.textColor = RGBOF(0x222222);
    self.nicknameLabel.font = [UIFont systemFontOfSize:16];
    [self.upContentView addSubview:self.nicknameLabel];

    //描述
    self.miaoshuLabel = [[UILabel alloc] init];
    self.miaoshuLabel.textColor = RGBOF(0x666666);
    self.miaoshuLabel.font = [UIFont systemFontOfSize:14];
    [self.upContentView addSubview:self.miaoshuLabel];
    
    //登录注册
    self.loginOrRegisImageView = [[UIImageView alloc] init];
    self.loginOrRegisImageView.image = [UIImage imageNamed:@"loginOrRegis"];
    [self.upContentView addSubview:self.loginOrRegisImageView];
    
    self.bottomView = [[UIView alloc] init];
    self.bottomView.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.bottomView];
    
    NSArray *titles = @[@"主页", @"关注", @"收藏", @"历史"];
    NSArray *images = @[@"wd_zy", @"wd_gz", @"wd_sc", @"wd_ls"];

    for (int i = 0; i < 4; i++) {
        JMBaseButtonConfig *buttonConfig = [JMBaseButtonConfig buttonConfig];
        buttonConfig.backgroundColor = [UIColor whiteColor];
        buttonConfig.title = titles[i];
        buttonConfig.titleColor = RGBOF(0x333333);
        buttonConfig.image = [UIImage imageNamed:images[i]];
        buttonConfig.styleType = JMButtonStyleTypeTop;
        JMButton *btn = [[JMButton alloc] initWithFrame:CGRectMake(i * (kWindowW / 4), 0, kWindowW / 4, 80) ButtonConfig:buttonConfig];
        btn.tag = i;
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.bottomView addSubview:btn];
        [self.btnsArr addObject:btn];
    }
}

- (void)btnClick:(UIButton *)sender{
    if (![YLUserInfo isLogIn]) {
        LoginViewController *login = [[LoginViewController alloc] init];
        [[AppDelegate sharedAppDelegate] pushViewController:login];
        return;
    }
    if (sender.tag == 0) {
        //个人主页
        CYGeRenZhuYeViewController *zhuye = [[CYGeRenZhuYeViewController alloc] init];
        zhuye.userId = [YLUserInfo shareUserInfo].userId;
        [[AppDelegate sharedAppDelegate] pushViewController:zhuye];
    }else if (sender.tag == 1){
        //guanzhu
        CYMine_WoDeGuanZhuViewController *guanzhu = [[CYMine_WoDeGuanZhuViewController alloc] init];
        [[AppDelegate sharedAppDelegate] pushViewController:guanzhu];

    }else if (sender.tag == 2){
        //collection
        CYMine_MyCollectionViewController *coll = [[CYMine_MyCollectionViewController alloc] init];
        [[AppDelegate sharedAppDelegate] pushViewController:coll];

    }else{
        CYMyHistoryViewController *history = [[CYMyHistoryViewController alloc] init];
        [[AppDelegate sharedAppDelegate] pushViewController:history];
    }
}

- (void)setIsLogin:(BOOL)isLogin{
    _isLogin = isLogin;
    if (_isLogin) {
        self.loginOrRegisImageView.hidden = YES;
        
        self.headImageView.hidden = NO;
        self.nicknameLabel.hidden = NO;
        self.miaoshuLabel.hidden = NO;
        
    }else{
        self.loginOrRegisImageView.hidden = NO;

        self.headImageView.hidden = YES;
        self.nicknameLabel.hidden = YES;
        self.miaoshuLabel.hidden = YES;
    }
    
    NSArray *selimages = @[@"wd_zy", @"wd_gz", @"wd_sc", @"wd_ls"];
    NSArray *norimages = @[@"log_out_homepage", @"log_out_ attention", @"log_out_ collect", @"log_out_ history"];
    NSArray *titles = @[@"主页", @"关注", @"收藏", @"历史"];

    
    [self.bottomView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    [self.btnsArr removeAllObjects];
    if (_isLogin) {
        for (int i = 0; i < 4; i++) {
            JMBaseButtonConfig *buttonConfig = [JMBaseButtonConfig buttonConfig];
            buttonConfig.backgroundColor = [UIColor whiteColor];
            buttonConfig.title = titles[i];
            buttonConfig.titleColor = RGBOF(0x333333);
            buttonConfig.image = [UIImage imageNamed:selimages[i]];
            buttonConfig.styleType = JMButtonStyleTypeTop;
            JMButton *btn = [[JMButton alloc] initWithFrame:CGRectMake(i * (kWindowW / 4), 0, kWindowW / 4, 80) ButtonConfig:buttonConfig];
            btn.tag = i;
            [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
            [self.bottomView addSubview:btn];
            [self.btnsArr addObject:btn];
        }
        
    }else{
        for (int i = 0; i < 4; i++) {
            JMBaseButtonConfig *buttonConfig = [JMBaseButtonConfig buttonConfig];
            buttonConfig.backgroundColor = [UIColor whiteColor];
            buttonConfig.title = titles[i];
            buttonConfig.titleColor = RGBOF(0x333333);
            buttonConfig.image = [UIImage imageNamed:norimages[i]];
            buttonConfig.styleType = JMButtonStyleTypeTop;
            JMButton *btn = [[JMButton alloc] initWithFrame:CGRectMake(i * (kWindowW / 4), 0, kWindowW / 4, 80) ButtonConfig:buttonConfig];
            btn.tag = i;
            [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
            [self.bottomView addSubview:btn];
            [self.btnsArr addObject:btn];
        }
        
    }
}

- (void)layoutSubviews{
    //底部视图
    [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).with.mas_offset(0);
        make.right.equalTo(self).with.mas_offset(0);
        make.bottom.equalTo(self).with.mas_offset(-10);
        make.height.mas_equalTo(80);
    }];
    //顶部视图
    [self.upView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).with.mas_offset(0);
        make.top.equalTo(self).with.mas_offset(0);
        make.right.equalTo(self).with.mas_offset(0);
        make.bottom.equalTo(self.bottomView.mas_top).with.mas_offset(0);
    }];
    
    //背景1
    [self.upImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.upView).with.mas_offset(0);
        make.left.equalTo(self.upView).with.mas_offset(0);
        make.right.equalTo(self.upView).with.mas_offset(0);
        make.height.mas_equalTo(140 + Height_NavBar);
    }];
    //内容
    [self.upContentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.upView).with.mas_offset(0);
        make.left.equalTo(self.upView).with.mas_offset(15);
        make.right.equalTo(self.upView).with.mas_offset(-15);
        make.height.mas_equalTo(112);
    }];
    //头像
    [self.headImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.upContentView).with.mas_offset(30);
        make.height.mas_equalTo(50);
        make.width.mas_equalTo(50);
        make.centerY.mas_equalTo(self.upContentView.centerY);
    }];
    //名字
    [self.nicknameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.headImageView.mas_right).with.mas_offset(13);
        make.right.equalTo(self.upContentView).with.mas_offset(-15);
        make.top.equalTo(self.upContentView).with.mas_offset(35);

    }];
    //描述
    [self.miaoshuLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.headImageView.mas_right).with.mas_offset(13);
        make.right.equalTo(self.upContentView).with.mas_offset(-15);
        make.top.equalTo(self.nicknameLabel.mas_bottom).with.mas_offset(10);
    }];
    //登录注册
    [self.loginOrRegisImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(34);
        make.width.mas_equalTo(110);
        make.center.mas_equalTo(self.upContentView.center);
    }];

    

}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    CGPoint point = [[touches anyObject] locationInView:self];
    //convert point to the white layer's coordinates
    point = [self.loginOrRegisImageView.layer convertPoint:point fromLayer:self.layer];
    //get layer using containsPoint:
    if ([self.loginOrRegisImageView.layer containsPoint:point]) {
        //convert point to blueLayer’s coordinates
        if (!self.isLogin) {
            CGFloat x = point.x;
            if (x < 55) {
                //登录
                NSLog(@"denglu");
                LoginViewController *login = [[LoginViewController alloc] init];
                [[AppDelegate sharedAppDelegate] pushViewController:login];
            }else{
                NSLog(@"注册");
                ZTRegisterViewController *regist = [[ZTRegisterViewController alloc] init];
                [[AppDelegate sharedAppDelegate] pushViewController:regist];
            }
        }
        
    }else{
        
    }
}

- (NSMutableArray *)btnsArr{
    if (!_btnsArr) {
        _btnsArr = [NSMutableArray arrayWithCapacity:0];
    }
    return _btnsArr;
}

@end
